package com.example.neteasecloudmusic_kotlin.ui.views

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewConfiguration
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout

class SuperSwipeRefreshLayout(context: Context, attrs: AttributeSet?) :
    SwipeRefreshLayout(context, attrs) {

    var startY : Float = 1.0.toFloat()
    var startX : Float = 1.0.toFloat()
    var mIsVpDragger : Boolean = false
    var mTouchSlop : Int = 1

    fun SuperSwipeRefreshLayout(context: Context, attrs: AttributeSet?) {
        mTouchSlop = ViewConfiguration.get(context).scaledTouchSlop
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        val action = ev?.action
        when (action) {
            MotionEvent.ACTION_DOWN -> {
                startY = ev.getY()
                startX = ev.getX()
                mIsVpDragger = false
            }
            MotionEvent.ACTION_MOVE -> {
                if (mIsVpDragger) return false
                var endY = ev.getY()
                var endX = ev.getX()
                var distanceX = Math.abs(endX - startX)
                var distanceY = Math.abs(endY - startY)
                if (distanceX > mTouchSlop && distanceX > distanceY) {
                    mIsVpDragger = true
                    return false
                }
            }
            MotionEvent.ACTION_CANCEL -> {
                mIsVpDragger = false
            }
        }
        return super.onInterceptTouchEvent(ev)
    }

}